/*
	Sodan Cloaking Device for GarrysMod10
	Copyright (C) 2007  Catdaemon

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");
SWEP.Sounds = {Engage=Sound("weapons/sodan_cloak_on.mp3"),Disengage=Sound("weapons/sodan_cloak_off.mp3")};

/****************************************************/
/*---------------------------Weapon Initialization-------------------*/
/****************************************************/
function SWEP:Initialize()
	self:SetWeaponHoldType("pistol");
	self.Owner:SetNWBool("shielded",false);
	self.ammo = 7
	self.Maxammo = 7

end


/****************************************************/
/*---------------------------Sets the 'ammo'----------------------------*/
/****************************************************/
function SWEP:SetPower(n)
	self.ammo = n
end



/****************************************************/
/*---------------------------Activates the Shield----------------------------*/
/****************************************************/
function SWEP:ActivateShield(ply, wep)
	ply:SetNWBool("shielded",true);
	ply:EmitSound(wep.Sounds.Engage,90,math.random(97,103));
	ply:SetMaterial("models/props_combine/com_shield001a")
	ply:GodEnable()
end



/****************************************************/
/*--------------------------De-Activates the Shield----------------------------*/
/****************************************************/
function SWEP:DeactivateShield(ply, wep)
	ply:SetNWBool("shielded",false);
	ply:EmitSound(wep.Sounds.Disengage,90,math.random(97,103));
	ply:SetMaterial("")
	ply:GodDisable()
end



/****************************************************/
/*---------------------------Primary Fire--------------------------------*/
/****************************************************/
function SWEP:PrimaryAttack()
	if not(self.Owner:GetNWBool("shielded")) then
		self:ActivateShield(self.Owner, self)
		return true;
	end
end



/****************************************************/
/*---------------------------Secondary Fire----------------------------*/
/****************************************************/
function SWEP:SecondaryAttack()
	if(self.Owner:GetNWBool("shielded")) then
		self:DeactivateShield(self.Owner, self)
		return true;
	end
end


/****************************************************/
/*---------------Decloak those who fail the criteria---------------*/
/****************************************************/
timer.Create("checkShieldCriteria",0.5,((9999999999999999999999999999999999*999999999999999999)*99999999999999999999),
	function()
		for _,v in pairs(player.GetAll()) do
			if(ValidEntity(v) and v:Team() == 3 and v:Alive()) then
				
					if(v:GetNWBool('shielded')) then
						if(v:GetWeapon("shield_gun").ammo > 1) then
							v:GetWeapon("shield_gun"):SetPower((v:GetWeapon("shield_gun").ammo - 1))--This is where we remove ammo
							//Msg(v:GetWeapon("shield_gun").ammo.."\n")
						end
						
					else
						if(v:GetWeapon("shield_gun").ammo < v:GetWeapon("shield_gun").Maxammo) then
							v:GetWeapon("shield_gun"):SetPower((v:GetWeapon("shield_gun").ammo + 0.333333))--This is where we add ammo
							//Msg(v:GetWeapon("shield_gun").ammo.."\n")
						end
					end
					
			if((v:GetWeapon("shield_gun").ammo <= 1 or v:GetActiveWeapon() != v:GetWeapon("shield_gun")) and v:GetNWBool('shielded')) then
						v:GetWeapon("shield_gun"):DeactivateShield(v, v:GetWeapon("shield_gun"))
						v:GetWeapon("shield_gun"):SetPower((v:GetWeapon("shield_gun").ammo + 0.33333333))--This is where we add ammo
							
			end
					
					
					

			end
		end
	end
);
